<!-- 角色管理 -->
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import type { IDbAction, IDbSearchConfig, IDbTable, ITagData } from '@/types/global'
import useNotic from '@/hooks/useNotic'
import * as roleApi from '@/api/role'
import { ENUM_BTN_AUTH_SYS } from '@/config/btn'
import { ENUM_ROLE } from '@/config/enum'
import { useSysStore } from '@/stores/sys'
import { ROLE_STATUS_LIST } from './config'
import RoleInfo from './comps/RoleInfo.vue'

const sysStore = useSysStore()
// 搜索栏配置
const searchConfig = reactive<IDbSearchConfig[]>([
  { label: '角色名称', prop: 'roleName', type: 'input' },
  { label: '权限字符', prop: 'roleKey', type: 'input' }
])

// 表格数据
const tableData = reactive<IDbTable>({
  columns: [
    { label: '序号', prop: '', type: 'index', width: '60px' },
    { label: '角色编号', prop: 'roleId', minWidth: '100px' },
    { label: '角色名称', prop: 'roleName', minWidth: '100px' },
    { label: '权限字符', prop: 'roleKey', minWidth: '100px' },
    {
      label: '状态',
      prop: 'status',
      width: '100px',
      type: 'tag',
      tagFunc: (scoped) => {
        const curStatus = ROLE_STATUS_LIST.find((item) => item.value == scoped.row.status)
        const params: ITagData = { label: curStatus?.label || '', type: curStatus?.tagType || '' }
        return params
      }
    },
    { label: '创建时间', prop: 'createTime', width: '200px' },
    { label: '操作', prop: 'handle', fixed: 'right', type: 'slot', width: '150px' }
  ],
  data: [],
  total: 0,
  search: {
    pageNum: 1,
    pageSize: 20
  }
})

// 弹窗数据
const roleInfoRef = ref<InstanceType<typeof RoleInfo>>()
const dialogData = reactive({
  title: '', // 角色弹窗标题
  disabled: false // 是否禁用
})

getDataList()

/**
 * 获取表格数据
 */
function getDataList() {
  roleApi.getRoleByPage(tableData.search).then((res) => {
    if (res.success) {
      tableData.total = res.data.total
      tableData.data = res.data.rows
    }
  })
}
/**
 * 点击添加
 */
function addClick() {
  dialogData.title = '添加角色'
  dialogData.disabled = false
  roleInfoRef.value?.init()
}
/**
 * 点击详情/编辑
 */
function infoClick(row: any, disabled: boolean) {
  dialogData.title = disabled ? '角色详情' : '编辑角色'
  dialogData.disabled = disabled
  roleInfoRef.value?.init(row.roleId)
}
/**
 * 点击删除
 */
function delClick(scoped: any) {
  useNotic().confirm(`您确定要删除第${scoped.$index + 1}个角色吗？`, (flag) => {
    if (flag) {
      roleApi.delRole(scoped.row.roleId).then((res) => {
        if (res.success) {
          useNotic().success('删除成功')
          getDataList()
        }
      })
    }
  })
}
/**
 * 设置表格按钮
 */
function setBtnList(scoped: any) {
  let list: IDbAction[] = [
    { label: '详情', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:查'], onClick: () => infoClick(scoped.row, true) }
  ]
  // 平台管理员：可以编辑、删除所有角色
  if (ENUM_ROLE['平台管理员'] == sysStore.roleKey) {
    list = [
      { label: '详情', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:查'], onClick: () => infoClick(scoped.row, true) },
      { label: '编辑', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:改'], onClick: () => infoClick(scoped.row, false) },
      { label: '删除', type: 'danger', auth: ENUM_BTN_AUTH_SYS['角色:删'], onClick: () => delClick(scoped) }
    ]
  }
  // 应用管理员：可以编辑、删除除平台管理员之外的所有角色
  else if (ENUM_ROLE['应用管理员'] == sysStore.roleKey) {
    if (scoped.row.roleKey != ENUM_ROLE['平台管理员']) {
      list = [
        { label: '详情', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:查'], onClick: () => infoClick(scoped.row, true) },
        { label: '编辑', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:改'], onClick: () => infoClick(scoped.row, false) },
        { label: '删除', type: 'danger', auth: ENUM_BTN_AUTH_SYS['角色:删'], onClick: () => delClick(scoped) }
      ]
    }
  }
  // 其他用户：只能编辑删、除自己
  else if (scoped.row.roleKey == sysStore.roleKey) {
    list = [
      { label: '详情', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:查'], onClick: () => infoClick(scoped.row, true) },
      { label: '编辑', type: 'primary', auth: ENUM_BTN_AUTH_SYS['角色:改'], onClick: () => infoClick(scoped.row, false) },
      { label: '删除', type: 'danger', auth: ENUM_BTN_AUTH_SYS['角色:删'], onClick: () => delClick(scoped) }
    ]
  }

  return list
}
</script>

<template>
  <dui-page :search-config="searchConfig" :search-data="tableData.search" @search="getDataList">
    <template #btn>
      <db-auth-btn :auth="ENUM_BTN_AUTH_SYS['角色:增']" type="primary" iconType="add" @click="addClick"> 添加角色 </db-auth-btn>
    </template>
    <template #default>
      <dui-table
        :data="tableData.data"
        :columns="tableData.columns"
        :total="tableData.total"
        :search="tableData.search"
        @search="getDataList"
      >
        <template #handleSlot="scoped">
          <dui-action :list="setBtnList(scoped)" />
        </template>
      </dui-table>
      <!-- 角色信息弹窗 -->
      <RoleInfo ref="roleInfoRef" v-bind="dialogData" @refresh="getDataList" />
    </template>
  </dui-page>
</template>
